package com.banciyuan.wallpaper.services;

import android.app.IntentService;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.media.ThumbnailUtils;
import android.text.TextUtils;
import android.view.Display;
import android.view.WindowManager;

import com.banciyuan.wallpaper.api.CoreApi;
import com.banciyuan.wallpaper.bean.Item;
import com.banciyuan.wallpaper.config.retrofit.RetrofitFactory;
import com.banciyuan.wallpaper.setting.SettingConfig;
import com.banciyuan.wallpaper.util.HttpUtils;
import com.banciyuan.wallpaper.util.JsonStatus;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Retrofit;
import rx.Observer;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by squall on 2016/8/1.
 */
public class ChangeSevices extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public ChangeSevices(String name) {
        super(name);
    }

    public ChangeSevices() {
        super("");
    }


    @Override
    protected void onHandleIntent(Intent intent) {


        final WallpaperManager wpManager = WallpaperManager.getInstance(this);

        Map<String, String> map = new HashMap<>();
        map.put("type", SettingConfig.ReadSettingString(getBaseContext(), SettingConfig.SEX));

        Retrofit retrofit = RetrofitFactory.creatRetrofit(getBaseContext());
        CoreApi coreApi = retrofit.create(CoreApi.class);
        coreApi.getSingle(HttpUtils.getData(map))
                .subscribeOn(Schedulers.immediate())
                .map(new Func1<JsonObject, Item>() {
                    @Override
                    public Item call(JsonObject jsonObject) {
                        Gson gson = new Gson();
                        if (JsonStatus.getJsonStatus(jsonObject)) {
                            String result = jsonObject.get("data").toString();
                            return gson.fromJson(result, Item.class);
                        } else {
                            return null;
                        }
                    }
                })
                .observeOn(Schedulers.immediate())
                .subscribe(new Observer<Item>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Item item) {
                        try {
                            if (!TextUtils.isEmpty(item.getImg_src())) {
                                int height = wpManager.getDesiredMinimumHeight();
                                int width = getScreen().x;
                                Bitmap resource = Glide.with(getBaseContext()).load(item.getImg_src()).asBitmap().into(width, height).get();
                                Bitmap result = ThumbnailUtils.extractThumbnail(resource, width, height);
                                wpManager.setBitmap(result);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private Point getScreen() {
        WindowManager wm = (WindowManager) getBaseContext().getSystemService(getBaseContext().WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        return point;
    }

}
